package com.nana.framework.ui.general
{
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class TextFieldFactory
	{
		private static var _instance:TextFieldFactory;
		
		private var _styleClass:Class;
		private var _stytleInstance:DisplayObject;
		private var _styleDic:Dictionary;
		private var _fontName:String;
		private var _fontEmbedType:String;
		private var _styleXml:XML;
		
		public function TextFieldFactory(){
			if (_instance == null){
				super();
				_instance = this;
			} else {
				throw (new Error("实例化单例类出错-TextFieldFactory"));
			}
		}
		
		public static function getInstance():TextFieldFactory{
			if (_instance == null){
				_instance = new TextFieldFactory();
			}
			return _instance;
		}
		
		public function registerStyle(styleClass:Class, styleXML:XML, fontName:String, embedType:String):void{
			this._styleClass = styleClass;
			this._stytleInstance = new this._styleClass();
			this._styleXml = styleXML;
			this._fontName = fontName;
			this._fontEmbedType = embedType;
		}
		
		public function getTextField(_arg1:String, _arg2:Array=null):TextField{
			var _local4:String = this.getStyleTextFieldName(_arg1);
			var _local5:Boolean = this.getUseEmbed(_arg1);
			var _local6:String = UIManager.getLanguageManager().getText(_arg1, _arg2);
			var _local3:TextField = this.copyTextField(this._stytleInstance[_local4], _local5, this._fontName);
			_local3.selectable = false;
			if (_local3){
				_local3.htmlText = _local6;
			}
			return (_local3);
		}
		
		private function getStyleTextFieldName(_arg1:String):String{
			var list:* = null;
			var resourceName:* = _arg1;
			list = this._styleXml.item.(@key == resourceName);
			return (list[0].@style);
		}
		
		private function getUseEmbed(_arg1:String):Boolean{
			var list:* = null;
			var useEmbedStr:* = null;
			var resourceName:* = _arg1;
			list = this._styleXml.item.(@key == resourceName);
			useEmbedStr = list[0].@useEmbed;
			if (useEmbedStr == "1"){
				return (true);
			}
			if (useEmbedStr == "2"){
				return (true);
			}
			return (false);
		}
		
		private function copyTextField(textField:TextField, embed:Boolean=false, font:String=""):TextField{		
			var filters:Array = [];			
			for (var i:int = 0; i < textField.filters.length; i++) {
				filters.push(textField.filters[i].clone());
			}
			
			var newTextField:TextField = new TextField();
			var textFormat:TextFormat = textField.defaultTextFormat;
			if (embed){
				textFormat.font = font;
				newTextField.embedFonts = true;
			}
			newTextField.defaultTextFormat = textFormat;
			newTextField.transform = textField.transform;
			newTextField.filters = filters;
			return newTextField;
		}
	}
}